//
//  HMhttpSession.swift
//  Swift网络框架
//
//  Created by 李立达 on 15/12/7.
//  Copyright © 2015年 heima11. All rights reserved.
//

import UIKit
import AFNetworking


enum HMRequestMethod : String
{
    case GET = "GET"
    case POST = "POST"
}

class HMhttpSession: AFHTTPSessionManager {
    
    
    
    static let sharanetWork : HMhttpSession = {
    
        let tools = HMhttpSession()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
        }()
    
  
        typealias HMhttPSessinCallBack = (response: AnyObject? , error:NSError?)->()
    
     func request(method : HMRequestMethod , url : String,  parmers : AnyObject? , callBack : (response: AnyObject? , error:NSError? )->() ) {
    
        
        
        let sucessBack = { (dataTask :NSURLSessionDataTask?, respons : AnyObject?) -> Void in
            
            callBack(response: respons, error: nil)
        }
        
        let failedBack = { (dataTask :NSURLSessionDataTask?, error :NSError ) -> Void in
            
            callBack(response: nil, error: error)
        }
        
        
        
        if method == .GET {
        
            self.GET(url, parameters: parmers, success: sucessBack, failure: failedBack)
        }
        else
        {
            self.POST(url, parameters: parmers, success: sucessBack, failure: failedBack)
        }
        
        
        
}
    
    private func request (method : HMRequestMethod , url : String , parmers : AnyObject? , sucess : (response : AnyObject?) -> (),failed : (error: NSError)->()) {
    
    
        if method == .GET {
        
            self.GET(url , parameters: parmers, success: { (_, respons) -> Void in
                
                sucess(response: respons)
                
                }, failure: { (_, error) -> Void in
                    
                   failed(error: error)
            })
            }
        
        }
    
    func loadStatus(access_Token : String , maxId : Int64=0,sinceId : Int64 = 0 ,callBack : HMhttPSessinCallBack) {
    
    
    let urlString = "https://api.weibo.com/2/statuses/friends_timeline.json"
    
      
        let parmers = [
        
            "access_token":access_Token,
            "max_id" : "\(maxId)",
            "since_id" : "\(sinceId)"
        ]
        request(.GET, url: urlString, parmers: parmers, callBack: callBack)
    }
    
}
